<template>
  <div class="exams-container">
    <el-card shadow="never" class="header-card">
      <div class="page-header">
        <div class="header-content">
          <el-icon class="header-icon" :size="28"><Edit /></el-icon>
          <div class="header-text">
            <h2>在线考试</h2>
            <p>课程测验和考核</p>
          </div>
        </div>
      </div>
    </el-card>

    <!-- 考试统计 -->
    <div class="stats-row">
      <el-card shadow="hover" class="stat-card">
        <div class="stat-content">
          <el-icon class="stat-icon" :size="40" color="#409EFF"><Document /></el-icon>
          <div class="stat-info">
            <div class="stat-value">{{ stats.total }}</div>
            <div class="stat-label">全部考试</div>
          </div>
        </div>
      </el-card>
      <el-card shadow="hover" class="stat-card">
        <div class="stat-content">
          <el-icon class="stat-icon" :size="40" color="#E6A23C"><Clock /></el-icon>
          <div class="stat-info">
            <div class="stat-value">{{ stats.pending }}</div>
            <div class="stat-label">待考试</div>
          </div>
        </div>
      </el-card>
      <el-card shadow="hover" class="stat-card">
        <div class="stat-content">
          <el-icon class="stat-icon" :size="40" color="#67C23A"><CircleCheck /></el-icon>
          <div class="stat-info">
            <div class="stat-value">{{ stats.completed }}</div>
            <div class="stat-label">已完成</div>
          </div>
        </div>
      </el-card>
      <el-card shadow="hover" class="stat-card">
        <div class="stat-content">
          <el-icon class="stat-icon" :size="40" color="#F56C6C"><TrendCharts /></el-icon>
          <div class="stat-info">
            <div class="stat-value">{{ stats.avgScore }}</div>
            <div class="stat-label">平均分</div>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 考试列表 -->
    <el-card shadow="never">
      <el-tabs v-model="activeTab">
        <el-tab-pane label="可参加" name="available" />
        <el-tab-pane label="已完成" name="completed" />
        <el-tab-pane label="全部" name="all" />
      </el-tabs>

      <el-table :data="filteredExams" stripe>
        <el-table-column prop="title" label="考试名称" min-width="200" />
        <el-table-column prop="courseName" label="所属课程" width="180" />
        <el-table-column prop="type" label="考试类型" width="120">
          <template #default="{ row }">
            <el-tag :type="getExamTypeTag(row.type)" size="small">
              {{ getExamTypeName(row.type) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="totalScore" label="总分" width="80" />
        <el-table-column prop="passScore" label="及格分" width="90" />
        <el-table-column prop="duration" label="时长" width="100" />
        <el-table-column label="考试时间" width="280">
          <template #default="{ row }">
            {{ row.startTime }} ~ {{ row.endTime }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)" size="small">
              {{ getStatusName(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button
              link
              type="primary"
              @click="handleStartExam(row)"
              v-if="row.status === 'pending'"
            >
              <el-icon><Edit /></el-icon>
              开始考试
            </el-button>
            <el-button
              link
              type="success"
              @click="handleViewResult(row)"
              v-if="row.status === 'completed'"
            >
              <el-icon><View /></el-icon>
              查看成绩
            </el-button>
            <el-button link @click="handleViewDetail(row)">
              <el-icon><Document /></el-icon>
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-empty v-if="filteredExams.length === 0" description="暂无考试" />
    </el-card>

    <!-- 考试结果对话框 -->
    <el-dialog v-model="showResultDialog" title="考试成绩" width="600px">
      <div v-if="currentExam" class="exam-result">
        <el-result
          :icon="currentExam.score >= currentExam.passScore ? 'success' : 'error'"
          :title="currentExam.score >= currentExam.passScore ? '恭喜通过！' : '未通过'"
        >
          <template #sub-title>
            <div class="result-info">
              <div class="score-display">
                <span class="score-label">得分：</span>
                <span class="score-value" :style="{ color: currentExam.score >= currentExam.passScore ? '#67C23A' : '#F56C6C' }">
                  {{ currentExam.score }}
                </span>
                <span class="score-total"> / {{ currentExam.totalScore }}</span>
              </div>
              <el-divider />
              <el-descriptions :column="2" border>
                <el-descriptions-item label="考试名称">{{ currentExam.title }}</el-descriptions-item>
                <el-descriptions-item label="及格分">{{ currentExam.passScore }}</el-descriptions-item>
                <el-descriptions-item label="考试时长">{{ currentExam.duration }}</el-descriptions-item>
                <el-descriptions-item label="完成时间">{{ currentExam.completeTime }}</el-descriptions-item>
              </el-descriptions>
            </div>
          </template>
          <template #extra>
            <el-button type="primary" @click="showResultDialog = false">确定</el-button>
          </template>
        </el-result>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'
import {
  DataLine, Clock, CircleCheck, TrendCharts, Medal, StarFilled,
  Edit, View, Document
} from '@element-plus/icons-vue'

// 状态
const activeTab = ref('available')
const showResultDialog = ref(false)
const currentExam = ref(null)
const trendPeriod = ref('month')

// 统计数据
const stats = reactive({
  total: 15,
  pending: 3,
  completed: 12,
  avgScore: 88
})

// 考试数据
const exams = ref([
  {
    id: 1,
    title: 'Vue 3 基础知识测验',
    courseName: 'Vue 3 从入门到精通',
    type: 'quiz',
    totalScore: 100,
    passScore: 60,
    duration: '60分钟',
    startTime: '2024-01-15 09:00',
    endTime: '2024-01-20 18:00',
    status: 'pending'
  },
  {
    id: 2,
    title: 'Composition API 考试',
    courseName: 'Vue 3 从入门到精通',
    type: 'exam',
    totalScore: 100,
    passScore: 70,
    duration: '90分钟',
    startTime: '2024-01-10 09:00',
    endTime: '2024-01-15 18:00',
    status: 'completed',
    score: 95,
    completeTime: '2024-01-12 10:30'
  },
  {
    id: 3,
    title: '产品设计能力测评',
    courseName: '产品思维与方法论',
    type: 'assessment',
    totalScore: 100,
    passScore: 60,
    duration: '120分钟',
    startTime: '2024-01-08 09:00',
    endTime: '2024-01-12 18:00',
    status: 'completed',
    score: 88,
    completeTime: '2024-01-10 14:20'
  }
])

// 计算属性
const filteredExams = computed(() => {
  if (activeTab.value === 'all') {
    return exams.value
  }
  return exams.value.filter(e => e.status === activeTab.value)
})

// 方法
const handleStartExam = (exam) => {
  ElMessage.info('正在进入考试...')
}

const handleViewResult = (exam) => {
  currentExam.value = exam
  showResultDialog.value = true
}

const handleViewDetail = (exam) => {
  ElMessage.info('查看考试详情：' + exam.title)
}

const getExamTypeName = (type) => {
  const map = {
    quiz: '课后测验',
    exam: '期末考试',
    assessment: '能力评估'
  }
  return map[type] || type
}

const getExamTypeTag = (type) => {
  const map = {
    quiz: '',
    exam: 'warning',
    assessment: 'success'
  }
  return map[type] || ''
}

const getStatusName = (status) => {
  const map = {
    pending: '待考试',
    completed: '已完成'
  }
  return map[status] || status
}

const getStatusType = (status) => {
  const map = {
    pending: 'warning',
    completed: 'success'
  }
  return map[status] || ''
}
</script>

<style scoped>
.exams-container {
  padding: 20px;
}

.header-card {
  margin-bottom: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.header-icon {
  color: #409EFF;
}

.header-text h2 {
  margin: 0;
  font-size: 24px;
  color: #303133;
}

.header-text p {
  margin: 4px 0 0 0;
  font-size: 14px;
  color: #909399;
}

.stats-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
  margin-bottom: 20px;
}

.stat-card {
  cursor: pointer;
  transition: all 0.3s;
}

.stat-card:hover {
  transform: translateY(-4px);
}

.stat-content {
  display: flex;
  align-items: center;
  gap: 20px;
}

.stat-info {
  flex: 1;
}

.stat-value {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 14px;
  color: #909399;
}

.data-card {
  transition: all 0.3s;
}

.data-card:hover {
  transform: translateY(-4px);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.trend-chart {
  height: 300px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chart-placeholder {
  font-size: 18px;
  color: #909399;
}

.record-card {
  margin-bottom: 12px;
}

.record-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.record-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 500;
  font-size: 15px;
}

.record-content p {
  margin: 0 0 8px 0;
  color: #606266;
}

.record-meta {
  display: flex;
  gap: 20px;
  font-size: 13px;
  color: #909399;
}

.exam-result {
  padding: 20px;
}

.result-info {
  padding: 20px 0;
}

.score-display {
  text-align: center;
  margin-bottom: 20px;
}

.score-label {
  font-size: 18px;
  color: #606266;
}

.score-value {
  font-size: 48px;
  font-weight: bold;
  margin: 0 10px;
}

.score-total {
  font-size: 24px;
  color: #909399;
}
</style>

